package com.huobao.yunying.activitys_log_reg;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.shangjia_version.activitys.Act_Work;
import com.huobao.yunying.activitys.Act_Main;
import com.huobao.yunying.activitys_hehuoren.Act_Hehuoren;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.Regions;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.services.RegTimeService;
import com.huobao.yunying.services.RegTimeUtils;
import com.huobao.yunying.utils.SelectAreaPopupWindow;
import com.huobao.yunying.utils.SessionUtils;
import com.huobao.yunying.utils.UserUtils;
import com.huobao.yunying.utils.Utils;
import com.huobao.yunying.views.ClearEditText;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.dialog.BaseDialog;
import com.xrz.sxm.base.utils.ExitUtil;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Act_Reg extends HuobaoBaseActivity implements OnClickListener {

	@ViewInject(R.id.tv_findpwd)
	private TextView tv_findpwd;

	@ViewInject(R.id.btn_sendyanzhengma)
	private Button btn_yanzheng;
	@ViewInject(R.id.edt_yanzhengma)
	private ClearEditText edt_yanzhengma;

	/////////////////////////////////////////
	// 手机号
	@ViewInject(R.id.edt_shoujihao)
	private ClearEditText edt_shoujihao;
	// 门店名字
	 @ViewInject(R.id.edt_store_name)
	 private ClearEditText edt_store_name;
	// 姓名
	@ViewInject(R.id.edt_name)
	private ClearEditText edt_name;
	// 详细地址
	// @ViewInject(R.id.edt_address)
	// private ClearEditText edt_address;
	// 密码
	@ViewInject(R.id.edt_pwd)
	private ClearEditText edt_pwd;
	// 重复密码
	// @ViewInject(R.id.edt_re_pwd)
	// private ClearEditText edt_re_pwd;

	// 登录按钮
	@ViewInject(R.id.tv_login)
	private TextView tv_login;

	// 发送信息的textview
	@ViewInject(R.id.tv_1)
	private TextView tv_msg;

	@ViewInject(R.id.tv_sheng)
	private TextView tv_sheng;

	// @ViewInject(R.id.tv_shi)
	// private TextView tv_shi;

	// 验证码那一列
	@ViewInject(R.id.llyt)
	private LinearLayout llyt_sms;

	@Override
	protected int getContentView() {
		return R.layout.layout_reg;
	}

	@Override
	protected String getTitleText() {
		return "注册";
	}

	@Override
	protected void init() {
		mAbTitleBar.setVisibility(View.GONE);
		EventBus.getDefault().register(this);
		// tv_findpwd.setOnClickListener(this);
		// tv_login.setOnClickListener(this);

		tv_sheng.setOnClickListener(this);
		// tv_shi.setOnClickListener(this);
		checkOpenSms();
		getRegions();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}

	private String m_Select_Sheng_name;
	private List<Regions> m_Selects_Shi;
	private String region_name, region_id;

	@Override
	public void onClick(View v) {
		final SelectAreaPopupWindow popupWindow = new SelectAreaPopupWindow();
		switch (v.getId()) {
		case R.id.tv_findpwd:
			Intent intent = new Intent(this, Act_FindPwd.class);
			startActivity(intent);
			finish();
			break;
		case R.id.tv_login:
			finish();
			break;
		case R.id.tv_sheng:
			popupWindow.showPop(context, m_AllRegions, v, new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					popupWindow.dismiss();
					// m_Selects_Shi = m_AllRegions.get(position).children;
					m_Select_Sheng_name = m_AllRegions.get(position).region_name;
					tv_sheng.setText(m_AllRegions.get(position).region_name);
					// if (m_Selects_Shi != null && m_Selects_Shi.size() != 0) {
					// tv_shi.setText("请选择市");
					// } else {
					region_id = m_AllRegions.get(position).region_id;
					region_name = m_Select_Sheng_name;
					// }

				}
			});
			break;
		// case R.id.tv_shi:
		// popupWindow.showPop(context, m_Selects_Shi, v, new
		// OnItemClickListener() {
		//
		// @Override
		// public void onItemClick(AdapterView<?> parent, View view, int
		// position, long id) {
		// popupWindow.dismiss();
		// region_id = m_Selects_Shi.get(position).region_id;
		// region_name = "中国 " + m_Select_Sheng_name +
		// m_Selects_Shi.get(position).region_name;
		// tv_shi.setText(m_Selects_Shi.get(position).region_name);
		// }
		// });
		// break;
		}

	}

	private boolean isOpenSms = true;

	private void checkOpenSms() {
		Map<String, String> map = HttpUtil.getParams("app", "login", "act", "issmscode");
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				isOpenSms = jsonObject.getBooleanValue("retval");
				if (isOpenSms) {
					llyt_sms.setVisibility(View.VISIBLE);
				} else {
					llyt_sms.setVisibility(View.GONE);
				}
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	private List<Regions> m_AllRegions = new ArrayList<Regions>();

	private void getRegions() {
		Map<String, String> map = HttpUtil.getParams("app", "regions");
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				JSONArray array = jsonObject.getJSONArray("retval");
				for (int i = 0; i < array.size(); i++) {
					JSONObject object = array.getJSONObject(i);
					Regions regions = new Regions();
					regions.region_id = object.getString("region_id");
					regions.region_name = object.getString("region_name");
					regions.children = JSON.parseArray(object.getString("children"), Regions.class);
					m_AllRegions.add(regions);
				}
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	public void onFasongYnazheng(View view) {
		String phone = edt_shoujihao.getText().toString().trim();
		if (Utils.isMobileNO(phone)) {
			sendYanzheng(phone);
		} else {
			Toast.makeText(context, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
		}
	}

	public void onRegClick(View view) {
		String agree = "1";
		final String phone_mob = edt_shoujihao.getText().toString().trim();
		String sms_code = edt_yanzhengma.getText().toString().trim();
		String user_name = edt_name.getText().toString().trim();
		 String shop_name = edt_store_name.getText().toString().trim();
		// String address = edt_address.getText().toString().trim();
		final String password = edt_pwd.getText().toString().trim();
		// String password_confirm = edt_re_pwd.getText().toString().trim();
		if (TextUtils.isEmpty(region_id)) {
			Toast.makeText(context, "请选择地区", Toast.LENGTH_SHORT).show();
			return;
		}
		if (TextUtils.isEmpty(phone_mob) == false && (TextUtils.isEmpty(sms_code) == false || isOpenSms == false)
				&& TextUtils.isEmpty(password) == false) {
			Map<String, String> map = HttpUtil.getParams("app", "member", "act", "register", "agree", agree,
					"phone_mob", phone_mob, "sms_code", sms_code, "user_name", user_name, "password", password,
					"region_id", region_id, "region_name", region_name,"shop_name",shop_name);
			Http.getInstant().sendRequest(context, map, new HttpListener() {

				@Override
				public void onHttpSuccess(String result) {
					Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show();
					onLoginClick(phone_mob, password);
				}

				@Override
				public void onHttpError(String error) {

				}
			});
		} else {
			Toast.makeText(context, "请将信息补充完整", Toast.LENGTH_SHORT).show();
		}
	}


	/**
	 * 登录按钮点击
	 * 
	 */
	private void onLoginClick(final String username, final String pwd) {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY,"login","user_name",username,"password",pwd);
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				int user_id = jsonObject.getIntValue("user_id");
				int user_role = jsonObject.getIntValue("user_role");
				UserUtils.save(context, user_id, user_role);
				SessionUtils.login(context, true);
				SessionUtils.saveUser(context, username, pwd,user_role);
				Toast.makeText(context, "登录成功！", Toast.LENGTH_LONG).show();
				//setResult(RESULT_OK);
				ExitUtil.getInstant().exitApp(context);
				switch(user_role){
					case 1:
						Intent intent2 = new Intent(context, Act_Main.class);
						intent2.putExtra("role",0);
						startActivity(intent2);
						break;
					case 2:
						Intent intent = new Intent(context, Act_Main.class);
						intent.putExtra("role",2);
						startActivity(intent);
						break;
					case 3:
						Intent intent1 = new Intent(context, Act_Hehuoren.class);
						startActivity(intent1);
						break;
				}
//				EventBus.getDefault().post(user_role, "LoginSuccess");
				finish();
			}
			
			@Override
			public void onHttpError(String error) {
			}
		});
	}
	
	
	
	
	private void sendYanzheng(final String phone) {
		Map<String, String> map = HttpUtil.getParams("app", "sms", "phone_mob", phone);
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				int retval = jsonObject.getIntValue("msg");
				String msg = jsonObject.getString("retval");
				switch (retval) {
				case 1:
				case 2:
				case 3:
					BaseDialog.showAlert(msg, context);
					break;
				case 4:
					Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
					startService(new Intent(context, RegTimeService.class));
					tv_msg.setText("验证码已发送至手机号" + phone);
					break;
				}
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	public void onBackclick(View v) {
		finish();
	}

	@Subscriber(tag = "regTime")
	public void onReceiverTime(int time) {
		if (time < 60) {
			btn_yanzheng.setEnabled(false);
			btn_yanzheng.setTextColor(Color.parseColor("#6E6E6E"));
			btn_yanzheng.setText((60 - time) + "秒后重新获取");
		} else {
			RegTimeUtils.clear(this);
			btn_yanzheng.setTextColor(Color.parseColor("#AE744C"));
			btn_yanzheng.setEnabled(true);
			btn_yanzheng.setText("发送验证码");
		}
	}
}
